<?php
/**
 * Maincategoryadmin_Data_Maincategorieslist
 * main category list
 * @author Adrian Stolarski
 */
class Maincategoryadmin_Data_Maincategorieslist implements 
Maincategoryadmin_Data_Mainable, 
        Maincategoryadmin_Data_Preparable, Maincategoryadmin_Data_Templatable, 
        Maincategoryadmin_Data_Tokenable{
    
    /**
     *
     * @var type $token
     * random token handler
     */
    public $token;
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_query
     * database query handler
     */
    public $database_query;
    
    /**
     * constructor()
     */
    public function  __construct(){
        $this->token = new Core_Data_Tokenizer;
        $this->database = new Core_Driver_MysqlImproved;
    }

    /**
     * create_token()
     * create random token
     * 
     * @return type Core_Data_Tokenizer
     */
    public function create_token() {
        return $this->token->create_token();
    }
    
    /**
     * get_template()
     * get current template
     * 
     * @return string $template
     */
    public function get_template_name() {
        $template = SERVER_ROOT."maincategoryadmin/views/maincategorylist.php";
        return $template;
    }
    
    /**
     * main()
     * initialise page data
     * @param array $variables
     */
    public function main(array $variables = null) {
        $this->database->connect();
        $this->prepare_query($variables);
        $this->database->prepare($this->database_query);
        $this->database->query();
        $page = array();
        $page[csrf_token] = $this->create_token();
        $page['template'] = $this->get_template_name();
        $page['main_category_data'] = array();
        //echo $this->database_query;
        //var_dump($page);
        while($main_category_data = $this->database->fetch('array')){
            $page['main_category_data'][$main_category_data['main_category_name']] = $main_category_data;
        }
        //var_dump($page);
        return $page;
    }
    
    /**
     * prepare_query();
     * prepare database query
     * 
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
       $this->database_query = "SELECT * FROM page_main_category, page_module WHERE page_main_category.main_category_module_id = page_module.id ORDER BY page_module.module_name ASC;"; 
    }

}
